﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class Sales
    {
        private string DBConnect = ConfigurationManager.ConnectionStrings["SS.Properties.Settings.SSZZConnectionString"].ToString();
        private string SP_GetSalesByProductName = "Sales_GetSalesByProductNameAndCustomerName";
        //private string SP_CheckProducts = "CheckProductsByName";
        private string SP_SaveSales = "Sales_SaveSales";
        private string SP_DeleteSales = "Sales_DeleteSales";
        //private string SP_GetAllSales = "Sales_GetAllSales";



        public DataTable GetSalesByProductName(string ProductName, string CustomerName)
        {
            DataTable dt = new DataTable();
            SqlConnection cn = new SqlConnection(DBConnect);
            SqlCommand cm = new SqlCommand();
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = SP_GetSalesByProductName;
            cm.Connection = cn;
            SqlParameter sqlParameterProductName = new SqlParameter("ProductName", ProductName);
            sqlParameterProductName.DbType = DbType.String;
            cm.Parameters.Add(sqlParameterProductName);
            SqlParameter sqlParameterCustomerName = new SqlParameter("CustomerName", CustomerName);
            sqlParameterCustomerName.DbType = DbType.String;
            cm.Parameters.Add(sqlParameterCustomerName);
            SqlDataAdapter da = new SqlDataAdapter(cm);
            da.Fill(dt);
            cn.Close();
            return dt;
        }

        public bool SaveSales(int SalesID, int ProductID, string SalesDescription, DateTime SalesDate, int SalesNumber, float TotalSales, float UnitPrice)
        {
            bool IsSave = true;
            SqlConnection cn = new SqlConnection(DBConnect);
            SqlCommand cm = new SqlCommand();
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = SP_SaveSales;
            cm.Connection = cn;
            SqlParameter sqlParameterSalesID = new SqlParameter("SalesID", SalesID);
            sqlParameterSalesID.DbType = DbType.Int32;
            cm.Parameters.Add(sqlParameterSalesID);
            SqlParameter sqlParameterProductID = new SqlParameter("ProductID", ProductID);
            sqlParameterProductID.DbType = DbType.Int32;
            cm.Parameters.Add(sqlParameterProductID);
            SqlParameter sqlParameterSalesDescription = new SqlParameter("SalesDescription", SalesDescription);
            sqlParameterSalesDescription.DbType = DbType.String;
            cm.Parameters.Add(sqlParameterSalesDescription);
            SqlParameter sqlParameterSalesDate = new SqlParameter("SalesDate", SalesDate);
            sqlParameterSalesDate.DbType = DbType.DateTime;
            cm.Parameters.Add(sqlParameterSalesDate);
            SqlParameter sqlParameterSalesNumber = new SqlParameter("SalesNumber", SalesNumber);
            sqlParameterSalesNumber.DbType = DbType.Int32;
            cm.Parameters.Add(sqlParameterSalesNumber);
            SqlParameter sqlParameterSalesAmout = new SqlParameter("TotalSales", TotalSales);
            sqlParameterSalesAmout.DbType = DbType.Double;
            cm.Parameters.Add(sqlParameterSalesAmout);
            SqlParameter sqlParameterUnitPrice = new SqlParameter("UnitPrice", UnitPrice);
            sqlParameterUnitPrice.DbType = DbType.Double;
            cm.Parameters.Add(sqlParameterUnitPrice);
            try
            {
                cn.Open();
                cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                IsSave = false;
            }
            cn.Close();
            return IsSave;
        }

        public bool DeleteSales(string SalesIDs)
        {
            bool IsDelete = true;
            SqlConnection cn = new SqlConnection(DBConnect);
            SqlCommand cm = new SqlCommand();
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = SP_DeleteSales;
            cm.Connection = cn;
            SqlParameter sqlParameterSalesIDs = new SqlParameter("SalesIDs", SalesIDs);
            sqlParameterSalesIDs.DbType = DbType.String;
            cm.Parameters.Add(sqlParameterSalesIDs);
            try
            {
                cn.Open();
                cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                IsDelete = false;
            }
            cn.Close();
            return IsDelete;
        }
    }
}
